#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   DeviceManage.py
@Time    :   2020/07/21 16:18:38
@Author  :   Xiong qiufeng
@Version :   1.0
@Contact :   1528399071@qq.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA
@Desc    :   None
'''

import PyQt5_stylesheets
import qtawesome as qta

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

from .DeviceAddDialog import DeviceAddDialog 
from .DeviceService import DeviceServiceJson

from ui.deviceManage import Ui_Form

import os
CONFIG_DEVICE_CMD = os.getcwd()+'/conf/device_cmd.json'

class DeviceManageWidget(QWidget, Ui_Form):

    devices = []

    def __init__(self, parent = None, save_path=''):
        super(QWidget, self).__init__(parent)
        self.setupUi(self)
        self.device_service = DeviceServiceJson()
        # 表格初始化
        self.tableWidgetInit()
        # 信号初始化
        self.signalInit()
        # 加载设备
        self.tableWidgetLoad()

    def signalInit(self):
        self.pushButton_add.clicked.connect(self.deviceAdd)
        self.pushButton_delete.clicked.connect(self.deviceDelete)
        self.pushButton_modify.clicked.connect(self.deviceModify)

    def tableWidgetInit(self):
        self.tableWidget.clicked.connect(self.tableWidgetItemClicked)
        self.tableWidget.setColumnCount(6)	
        self.tableWidget.setHorizontalHeaderLabels(['名称', '位置', '型号', 'sn', '订阅', '推送'])
        # 下面代码让表格100填满窗口
        self.tableWidget.horizontalHeader().setStretchLastSection(True)
        self.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        # 多选
        self.tableWidget.setSelectionBehavior(QAbstractItemView.SelectRows)
        # self.setSelectionMode(QAbstractItemView.ExtendedSelection)

    def tableWidgetItemClicked(self, item):
        self.select_row = item.row()

    def tableWidgetLoad(self):
        self.devices = self.device_service.getAll()
        items = self.devices
        self.tableWidget.clear()
        self.tableWidget.setRowCount(len(items))
        self.tableWidget.setHorizontalHeaderLabels(['名称', '位置', '型号', 'sn', '订阅', '推送'])

        for i, element in enumerate(self.devices):
            item = element['value']
            icon = QIcon(':icons/images/question.png')
            if item['model'] == 'FSC-01':
                icon = QIcon(':icons/images/guangji.png')
            if item['model'] == 'FSC-02':
                icon = QIcon(':icons/images/men.png')
            if item['model'] == 'FSC-03':
                icon = QIcon(':icons/images/hongwai.png')
            if item['model'] == 'FSC-04':
                icon = QIcon(':icons/images/fusheyuan.png')
            if item['model'] == 'FSC-05':
                icon = QIcon(':icons/images/guidao.png')
            self.tableWidget.setItem(i, 0, QTableWidgetItem(icon, str(item['name'])))
            self.tableWidget.setItem(i, 1, QTableWidgetItem(str(item['position'])))
            self.tableWidget.setItem(i, 2, QTableWidgetItem(str(item['model'])))
            self.tableWidget.setItem(i, 3, QTableWidgetItem(str(item['sn'])))
            self.tableWidget.setItem(i, 4, QTableWidgetItem(str(item['sub'])))
            self.tableWidget.setItem(i, 5, QTableWidgetItem(str(item['pub'])))

    def deviceAdd(self):
        device = self.devices[self.select_row]
        dlg = DeviceAddDialog()
        dlg.load(device)
        if dlg.exec() == 1:
            resp = self.device_service.add(dlg.device)
            if resp['code'] == 200:
                self.tableWidgetLoad()
                QMessageBox.information(self,"提示",resp['msg'], QMessageBox.Yes)
            else:
                QMessageBox.information(self,"提示",resp['msg'], QMessageBox.Yes)

    def deviceDelete(self):
        device = self.devices[self.select_row]
        info = '名称：%s\r\nSN：%s\r\n位置：%s\r\n型号：%s'%(device['value']['name'],device['value']['sn'], device['value']['position'], device['value']['model'])
        button = QMessageBox.question(self,"询问","确认删除设备? \r\n\r\n%s"%info,
                    QMessageBox.Ok|QMessageBox.Cancel, QMessageBox.Ok)
        if button == QMessageBox.Ok:
            if self.device_service.delete(device):
                self.tableWidgetLoad()
        elif button == QMessageBox.Cancel:
            return

    def deviceModify(self):
        device = self.devices[self.select_row]
        dlg = DeviceAddDialog()
        dlg.load(device)
        if dlg.exec() == 1:
            resp = self.device_service.modify(dlg.device)
            if resp['code'] == 200:
                self.tableWidgetLoad()
                QMessageBox.information(self,"提示",resp['msg'], QMessageBox.Yes)
            else:
                QMessageBox.information(self,"提示",resp['msg'], QMessageBox.Yes)

